package com.lou.springboot.controller;

import com.lou.springboot.entity.SaleGoods;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.expression.Dates;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;
import java.util.stream.Collectors;

@Controller
public class ThymeleafController {

    @GetMapping("/thymeleaf")
    public String hello(HttpServletRequest request,
                        @RequestParam(value = "description", required = false, defaultValue = "springboot-thymeleaf") String description){
        request.setAttribute("description", description);
        return "thymeleaf";
    }

    @GetMapping("/attributes")
    public String attributes(ModelMap map) {
        map.put("title", "Thymeleaf 标签演示");
        map.put("th_id", "thymeleaf-input");
        map.put("th_value", "thymeleaf-input");
        map.put("th_class", "thymeleaf-class");
        map.put("th_href", "http://13blog.site");
        return "attributes";
    }

    @GetMapping("/simple")
    public String simple(ModelMap map){
        map.put("thymeleafText", 2020);
        map.put("number1", 2019);
        map.put("number2", 20);
        map.put("thymeleafText1", "shiyanlou");
        SaleGoods saleGoods = new SaleGoods();
        saleGoods.setType(1);
        saleGoods.setOnSale(true);
        saleGoods.setWeight(23.1f);
        saleGoods.setGoodsName("西瓜");
        map.put("saleGoods", saleGoods);
        return "simple";
    }
    @GetMapping("/test")
    public String test(ModelMap map) {
        map.put("title", "Thymeleaf 语法测试");
        map.put("testString", "玩转 Spring Boot");
        map.put("bool", true);
        map.put("testArray", new Integer[]{2018,2019,2020,2021});
        map.put("testList", Arrays.asList("Spring", "Spring Boot", "Thymeleaf", "MyBatis", "Java"));
        Map testMap = new HashMap();
        testMap.put("platform", "shiyanlou");
        testMap.put("title", "玩转 Spring Boot");
        testMap.put("author", "十三");
        map.put("testMap", testMap);
        map.put("testDate", new Date());
        return "test";
    }

    // 实现了多文件上传
    public static final String FILE_UPLOAD_PATH = "/Users/daxin/Desktop/lou-springboot/src/main/resources/upload/";
    @PostMapping("/upload")
    @ResponseBody
    // "file" 名称需要和前端页面中添加文件的input的name保存一致
    public String upload(@RequestParam("file") MultipartFile[] files){
        if (files.length > 0) {
            List<String> newFileNames = new ArrayList<>();
            int len = files.length;
            for (int i = 0; i < len;i++){
                MultipartFile file = files[i];
                if (file.isEmpty()) {
                    return "上传文件失败";
                }
                String filename = file.getOriginalFilename();
                String suffixName = filename.substring(filename.lastIndexOf("."));
                // 生成文件名称通用方法
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
                Random r = new Random();
                StringBuilder tempName = new StringBuilder();
                tempName
                        .append(sdf.format(new Date()))
                        .append(r.nextInt(100))
                        .append(suffixName);
                String newFileName = tempName.toString();
                newFileNames.add(newFileName);
                try{
                    // 保存文件
                    byte[] bytes = file.getBytes();
                    Path path = Paths.get(FILE_UPLOAD_PATH + newFileName);
                    Files.write(path, bytes);
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            List<String> finalNewFileNames = newFileNames.stream().map(fileName -> "/files/" + fileName).collect(Collectors.toList());
            return "上传成功，图片地址为：" + finalNewFileNames;
//            return "上传成功";
        } else {
            return "上传失败";
        }
    }
}
